package com.adong.handler;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

/**
 * @author com.adong
 * date表示java中你想要转换的类型
 */

public class DateHandler extends BaseTypeHandler<Date> {
    //将java类型转换成数据库中的类型
    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, Date parameter, JdbcType jdbcType) throws SQLException{
        long time = parameter.getTime();
        ps.setLong(i,time);

    }

    //将数据库中类型转换成java类型
    //String数据库中要转换的字段名,ResultSet查询的结果集
    @Override
    public Date getNullableResult(ResultSet rs, String columnName) throws SQLException {
        //获取数据库中的值
        long aLong = rs.getLong(columnName);
        Date date = new Date(aLong);
        return date;
    }

    @Override
    public Date getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        long aLong = rs.getLong(columnIndex);
        Date date = new Date(aLong);
        return date;
    }

    @Override
    public Date getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        long aLong = cs.getLong(columnIndex);
        Date date = new Date(aLong);
        return date;
    }
}
